package com.heiyanqiang.dao;

import com.heiyanqiang.domain.Role;
import com.heiyanqiang.domain.UserInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface UserDao {


    //用户登录
    @Select("select * from users where username=#{username}")
    @Results({
           @Result(id = true,property = "id",column = "id"),
           @Result(property = "username",column = "username"),
           @Result(property = "email",column = "email"),
           @Result(property = "password",column = "password"),
           @Result(property = "phoneNum",column = "phoneNum"),
           @Result(property = "status",column = "status"),
           @Result(property = "roles",column = "id",javaType = java.util.List.class,many = @Many(select = "com.heiyanqiang.dao.RoleDao.findRoleByUserId")),
    })
    UserInfo findByUserName(String username) throws Exception;

    //用户管理
    @Select("select * from users")
    List<UserInfo> findAll() throws Exception;

    //添加用户
    @Insert("insert into users (email,username,password,phoneNum,status) values (#{email},#{username},#{password},#{phoneNum},#{status})")
    void save(UserInfo userInfo) throws Exception;

    //查询用户详情
    @Select("select * from users where id=#{id}")
    @Results({
            @Result(id = true,property = "id",column = "id"),
            @Result(property = "username",column = "username"),
            @Result(property = "email",column = "email"),
            @Result(property = "password",column = "password"),
            @Result(property = "phoneNum",column = "phoneNum"),
            @Result(property = "status",column = "status"),
            @Result(property = "roles",column = "id",javaType = java.util.List.class,many = @Many(select = "com.heiyanqiang.dao.RoleDao.findRoleByUserId")),
    })
    UserInfo findById(Integer id);

    //查询当前用户没哟的权限
    @Select("select * from role where id not in (select roleId from users_role where userId=#{userId})")
    List<Role> findOtherRoles(Integer userId);

    //用户关联角色 使用param给要添加的数据起个别名
    @Insert("insert into users_role (userId,roleId) values (#{userId},#{roleId})")
    void addRolesToUser(@Param("userId") Integer userId, @Param("roleId") Integer roleId);

}
